import Vue from 'vue';
import VueRouter from 'vue-router'


Vue.use(VueRouter);


import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'


import business from '@/views/layout/business/business'
import chart from '@/views/layout/chart/chart'
import question from '@/views/layout/question/question'
import subject from '@/views/layout/subject/subject'
import user from '@/views/layout/user/user'
// import { from } from 'core-js/fn/array';

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect:'/login'
    },
    {
      path: '/login',
      component:login
    },
    {
      path: '/layout',
      component: layout,
      redirect:'/layout/chart',
      children: [
        {
          path: '/layout/business',
          component: business,
          meta: {
            title:'business'
          }
        },
        {
          path: '/layout/chart',
          component: chart,
          meta: {
            title:'chart'
          }
        },
        {
          path: '/layout/question',
          component: question,
          meta: {
            title:'question'
          }
        },
        {
          path: '/layout/subject',
          component: subject,
          meta: {
            title:'subject'
          }
        },
        {
          path: '/layout/user',
          component: user,
          meta: {
            title:'user'
          }
        },
      ]
    }
  ]
})


//解决相同路径跳转报错
const originalPush = VueRouter.prototype.push 
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this,location).catch(err=>err)
}


import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
//路由导航守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach((to, from) => {

  document.querySelector('title').innerText = to.meta.title

  setInterval(() => {
    NProgress.done()
  },500)
  console.log(to, from);
})

export default router